#include "generatorwidget.h"
#include "ui_generatorwidget.h"
#include "yijugeneratorobject.h"
#include <QThread>
#include <QJsonObject>

GeneratorWidget::GeneratorWidget(QWidget *parent) :
    g(new YiJuGeneratorObject),
    t(new QThread(this)),
    m_proc(0),
    m_userData(0),
    QWidget(parent),
    ui(new Ui::GeneratorWidget)
{
    ui->setupUi(this);
    init();
}

GeneratorWidget::~GeneratorWidget()
{
    t->quit();
    t->wait(3000);
    delete ui;
}

void GeneratorWidget::init()
{
    connect(ui->msModeBtn, SIGNAL(toggled(bool)), this, SLOT(onMsMode(bool)));
    connect(ui->mxModeBtn, SIGNAL(toggled(bool)), this, SLOT(onMxMode(bool)));
    connect(ui->msModeBtn, SIGNAL(toggled(bool)), g, SLOT(onSetMsMode(bool)));
    connect(ui->mxModeBtn, SIGNAL(toggled(bool)), g, SLOT(onSetMxMode(bool)));
    connect(ui->fcsBtn, SIGNAL(toggled(bool)), g, SLOT(onSetFCS(bool)));
    connect(ui->fclBtn, SIGNAL(toggled(bool)), g, SLOT(onSetFCL(bool)));
    connect(ui->saveBtn, SIGNAL(clicked(bool)), this, SLOT(onSaveValues()));
    connect(ui->kvSpin, SIGNAL(valueChanged(double)), g, SLOT(onKvValue(double)));
    connect(ui->kvDecBtn, SIGNAL(clicked(bool)), g, SLOT(onKvDec()));
    connect(ui->kvIncBtn, SIGNAL(clicked(bool)), g, SLOT(onKvInc()));
    connect(ui->maSpin, SIGNAL(valueChanged(double)), g, SLOT(onMaValue(double)));
    connect(ui->maDecBtn, SIGNAL(clicked(bool)), g, SLOT(onMaDec()));
    connect(ui->maIncBtn, SIGNAL(clicked(bool)), g, SLOT(onMaInc()));
    connect(ui->msSpin, SIGNAL(valueChanged(double)), g, SLOT(onMsValue(double)));
    connect(ui->msDecBtn, SIGNAL(clicked(bool)), g, SLOT(onMsDec()));
    connect(ui->msIncBtn, SIGNAL(clicked(bool)), g, SLOT(onMsInc()));
    connect(ui->mxSpin, SIGNAL(valueChanged(double)), g, SLOT(onMxValue(double)));
    connect(ui->mxDecBtn, SIGNAL(clicked(bool)), g, SLOT(onMxDec()));
    connect(ui->mxIncBtn, SIGNAL(clicked(bool)), g, SLOT(onMxInc()));
    connect(this, SIGNAL(openSerialPort()), g, SLOT(onOpenSerialPort()));
    connect(g, SIGNAL(deviceStatus(GeneratorState,QString)),
            this, SLOT(onDeviceStatus(GeneratorState,QString)));
    connect(t, SIGNAL(finished()), g, SLOT(deleteLater()));
    g->moveToThread(t);
    t->start();
    emit openSerialPort();
}

void GeneratorWidget::setDoseValues(const double &kv, const double &ma, const double &ms, const double &mx)
{
    if (mx == 0) {
        ui->msModeBtn->setChecked(true);
        ui->kvSpin->setValue(kv);
        ui->maSpin->setValue(ma);
        ui->msSpin->setValue(ms);
        if (!g->isSerialPortOpened()) {
            ui->mxSpin->setValue(ma*ms/1000);
        }
    } else {
        ui->mxModeBtn->setChecked(true);
        ui->kvSpin->setValue(kv);
        ui->maSpin->setValue(ma);
        ui->msSpin->setValue(mx);
        if ((!g->isSerialPortOpened()) && ma != 0) {
            ui->msSpin->setValue(mx*1000/ma);
        }
    }
}

void GeneratorWidget::getDoseValues(double &kv, double &ma, double &ms, double &mx) const
{
    kv = ui->kvSpin->value();
    ma = ui->maSpin->value();
    ms = ui->msSpin->value();
    mx = ui->mxSpin->value();
    if (ui->msModeBtn->isChecked()) {
        mx = 0;
    } else if (ui->mxModeBtn->isChecked()) {
        ms = 0;
    }
}

void GeneratorWidget::onMsMode(bool checked)
{
    ui->msSpin->setEnabled(checked);
    ui->msIncBtn->setEnabled(checked);
    ui->msDecBtn->setEnabled(checked);
    ui->mxSpin->setEnabled(!checked);
    ui->mxIncBtn->setEnabled(!checked);
    ui->mxDecBtn->setEnabled(!checked);
}

void GeneratorWidget::onMxMode(bool checked)
{
    ui->msSpin->setEnabled(!checked);
    ui->msIncBtn->setEnabled(!checked);
    ui->msDecBtn->setEnabled(!checked);
    ui->mxSpin->setEnabled(checked);
    ui->mxIncBtn->setEnabled(checked);
    ui->mxDecBtn->setEnabled(checked);
}

void GeneratorWidget::onDeviceStatus(GeneratorState state, const QString &msg)
{
    double val;
    switch (state) {
    case GenKvValue:
        ui->kvSpin->setValue(msg.toDouble());
        break;
    case GenMaValue:
        ui->maSpin->setValue(msg.toDouble());
        break;
    case GenMsValue:
        val = msg.toDouble();
        ui->msSpin->setValue(val);
        ui->mxSpin->setValue((val*ui->maSpin->value())/1000);
        break;
    case GenMxValue:
        val = msg.toDouble();
        ui->mxSpin->setValue(val);
        if (ui->maSpin->value() > 0)
            ui->msSpin->setValue((val*1000)/(ui->maSpin->value()));
        break;
    case GenMxMode:
        ui->mxModeBtn->setChecked(true);
        break;
    case GenMsMode:
        ui->msModeBtn->setChecked(true);
        break;
    case GenFCL:
        ui->fclBtn->setChecked(true);
        break;
    case GenFCS:
        ui->fcsBtn->setChecked(true);
        break;
    case GenRFF:
        break;
    case GenFRY:
        break;
    case GenFRN:
        break;
    case GenFKvValue:
        break;
    case GenFMaValue:
        break;
    case GenFMsValue:
        break;
    case GenPreOn:
        ui->preLamp->setYellow();
        break;
    case GenPreOff:
        ui->preLamp->setGray();
        break;
    case GenReadyOn:
        ui->preLamp->setGreen();
        break;
    case GenReadyOff:
        ui->preLamp->setGray();
        break;
    case GenXRayOn:
        ui->xrayLamp->setRed();
        break;
    case GenXRayOff:
        ui->xrayLamp->setGray();
        break;
    case GenFaultOn:
        ui->faultLamp->setRed();
        break;
    case GenFaultOff:
        ui->faultLamp->setGreen();
        break;
    case GenError:
        if (m_proc) {
            QJsonObject json;
            json["action"] = "ShowMessage";
            json["args"] = msg;
            m_proc(json, m_userData);
        }
        break;
    case GenSerialPortOpened:
        ui->portLable->setText(g->getName() + tr(": ") + g->getPortName() + tr(" Opened"));
        break;
    case GenSerialPortOpenFailed:
        ui->portLable->setText(g->getName() + tr(": ") + g->getPortName() + tr(" Open failed"));
        if (m_proc) {
            QJsonObject json;
            json["action"] = "ShowMessage";
            json["args"] = ui->portLable->text();
            m_proc(json, m_userData);
        }
        break;
    case GenSerialPortClosed:
        ui->portLable->setText(g->getName() + tr(": ") + g->getPortName() + tr(" Closed"));
        break;
    }
}

void GeneratorWidget::onSaveValues()
{
    QJsonObject json, arg;
    json["action"] = "SaveDoseValues";
    arg["kv"] = ui->kvSpin->value();
    arg["ma"] = ui->maSpin->value();
    arg["ms"] = ui->msSpin->value();
    arg["mx"] = ui->mxSpin->value();
    if (ui->msModeBtn->isChecked()) {
        arg["mx"] = 0;
    } else if (ui->mxModeBtn->isChecked()) {
        arg["ma"] = 0;
        arg["ms"] = 0;
    }
    json["args"] = arg;
    if (m_proc) m_proc(json, m_userData);
}
